package com.ce.pms.system.custservice.web;

import java.util.List;

import com.ce.pms.base.vo.SearchVO;
import com.ce.pms.base.web.PmsBaseAction;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.system.custservice.service.ICustEventKindService;
import com.ce.pms.system.custservice.service.ICustEventNatureService;
import com.ce.pms.system.custservice.vo.CustEventKindVO;
import com.ce.pms.system.custservice.vo.CustEventNatureVO;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.annotation.Read;
import com.sitechasia.webx.core.support.Page;

/**
 * 事件类型
 * 
 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
 * @Created 2008-12-17
 */
public class SystemCustEventKindAction extends PmsBaseAction implements
		ISystemEventAction {
	private static final String		PAGE_EDIT	= "systemCustEventKindEditPage";

	private static final String		PAGE_INIT	= "systemCustEventKindListPage";

	private ICustEventKindService	custEventKindService;

	private ICustEventNatureService	custEventNatureService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ce.pms.system.custservice.web.ISystemEventAction#addPage()
	 */
	public String addPage() {
		List<CustEventNatureVO> eventNatureId = this.custEventNatureService
				.queryAllCustEventNature();

		this.setAttribute("eventNatureId", eventNatureId);

		return PAGE_EDIT;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ce.pms.system.custservice.web.ISystemEventAction#delete(java.lang
	 * .Long)
	 */
	public void delete(@Read(key = "keyword") Long keyword) {
		try {
			if (this.custEventKindService.deleteCustEventKindById(keyword)) {
				this.renderText("true");
			} else {
				this.renderText("false");
			}
		} catch (RuntimeException re) {
			re.printStackTrace();
			throw new BusinessException(
					"custservice:system_delete_inused_exception",
					BusinessException.WARNING);
		}
	}

	/**
	 * 编辑
	 * 
	 * @param custEventKindVO
	 * @author <a href="mailto:i0o@live.cn">黄腾飞</a>
	 * @created 2008-12-22
	 */
	public void edit(@Read CustEventKindVO custEventKindVO) {

		if (BlankUtil.isBlank(custEventKindVO.getKeyword())) {
			custEventKindVO.setCreator(this.getUserId());
			this.custEventKindService.addCustEventKind(custEventKindVO);
		} else {
			custEventKindVO.setModifier(this.getUserId());
			this.custEventKindService.editCustEventKind(custEventKindVO);
		}

		this.renderText("true");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ce.pms.system.custservice.web.ISystemEventAction#editPage()
	 */
	public String editPage(@Read(key = "keyword") Long keyword) {
		List<CustEventNatureVO> eventNatureId = this.custEventNatureService
				.queryAllCustEventNature();

		this.setAttribute("eventNatureId", eventNatureId);

		CustEventKindVO custEventKindVO = this.custEventKindService
				.findObjectVOById(keyword);

		this.setAttribute("custEventKindVO", custEventKindVO);

		return PAGE_EDIT;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ce.pms.system.custservice.web.ISystemEventAction#existsName(java.
	 * lang.String)
	 */
	public void existsName(@Read(key = "name") String name) {
		try {
			this.custEventKindService.checkExistenceByName(name);
			this.renderText("true");
		} catch (RuntimeException re) {
			this.renderText(this.getLocaleMessage(re.getMessage()));
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ce.pms.system.custservice.web.ISystemEventAction#initMain()
	 */
	public String initMain() {
		this.queryDataList(15, 1);
		return PAGE_INIT;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ce.pms.system.custservice.web.ISystemEventAction#queryDataList()
	 */
	public String queryDataList(
			@Read(key = "ec_crd", defaultValue = "15") int pageSize,
			@Read(key = "ec_p", defaultValue = "1") int pageNo) {
		SearchVO searchVO = new SearchVO();
		searchVO.setPageNo(pageNo);
		searchVO.setPageSize(pageSize);

		Page page = this.custEventKindService
				.queryCustEventKindListByPage(searchVO);

		this.setAttribute("queryDataList", page.getResult());
		this.setAttribute("totalRows", Integer.valueOf(String.valueOf(page
				.getTotalCount())));

		return PAGE_INIT;
	}

	/**
	 * @param custEventKindService the custEventKindService to set
	 */
	public void setCustEventKindService(
			ICustEventKindService custEventKindService) {
		this.custEventKindService = custEventKindService;
	}

	/**
	 * @param custEventNatureService the custEventNatureService to set
	 */
	public void setCustEventNatureService(
			ICustEventNatureService custEventNatureService) {
		this.custEventNatureService = custEventNatureService;
	}

}
